<template>
  <el-container class="container">
    <el-header class="home-header">
      <el-row>
        <el-col :span="8"><img src="../../assets/images/logo.png" alt=""></el-col>
        <el-col :span="8"><h1 class="home-title">电商后台管理系统</h1></el-col>
        <el-col class="exit" :span="8">欢迎上海前端32期星耀会员<a @click.prevent="exit" href="#">退出</a></el-col>
      </el-row>
    </el-header>
    <el-container class="home-bottom">
      <el-aside class="home-aside" width="200px">
        <!-- unique-opened: 是否只保持一个子菜单的展开 -->
        <el-menu :unique-opened='true' :router="true" :default-active="$route.path" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
          <el-submenu :index="item1.id+''" v-for="item1 in menus" :key="item1.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item1.authName}}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item :index="'/' + item2.path" v-for="item2 in item1.children" :key="item2.id">{{item2.authName}}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="home-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: {}
    }
  },
  // 获取菜单列表
  async created () {
    try {
      const res = await this.$axios.get('menus')
      this.menus = res.data.data
    } catch (error) {
      console.log('获取菜单列表失败')
    }
  },
  methods: {
    // 退出登录
    exit () {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        this.$message({
          type: 'success',
          message: '退出成功!',
          duration: 800
        })
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    }
  }
}
</script>

<style lang="less">
.container {
  height: 100%;
}
.home-header {
  height: 60px;
  padding: 0;
  background-color: #b3c1cd;
  .home-title {
    text-align: center;
    line-height: 60px;
    color: #fff;
    font-size: 28px;
  }
  .exit {
    text-align: right;
    line-height: 60px;
    padding-right: 30px;
    a {
      color: #daa520;
    }
  }
}
.home-bottom {
  height: 100px;
}
.home-aside {
  background-color: #545c64;
  height: 100%;
}
.home-main {
  background-color: #eaeef1;
}
.breadcrumb.el-breadcrumb {
  height: 50px;
  line-height: 50px;
  background-color: #d4dae0;
  padding-left: 20px;
  font-size: 16px;
}
</style>
